<template>
  <div>
    <van-nav-bar title="登录" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="user.mobile"
        name="手机号"
        label="手机号"
        placeholder="请输入手机号"
        required
        :rules="[{ required: true, message: '请填写手机号',pattern:/^1[3-9]\d{9}$/}]"
      />
      <van-field
        v-model="user.code"
        type="password"
        name="code"
        label="密码"
        required
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请填写密码', pattern:/^\d{6}$/}]" />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit" :loading="isLoading" loading-text="登录中..." :disabled="isLoading" >登录</van-button>
      </div>
    </van-form>
  </div>
</template>
<script>
import { Notify } from 'vant'
import { setToken } from '@/utils/token'
export default {
  name: 'loginC',
  data () {
    return {
      user: {
        // 手机号
        mobile: '',
        //   246810，密码
        code: ''
      },
      isLoading: false
    }
  },
  methods: {
    //   from的验证都通过才能提交
    async onSubmit (values) {
      this.isLoading = true
      try {
        const res = await this.$API.reqUerLogin({ ...this.user })
        console.log(res)
        Notify({ type: 'success', message: '登录成功' })
        setToken(res.data.data.token)
        localStorage.setItem('refresh_token', res.data.data.refresh_token)
        if (this.$route.query.redirect) {
          this.$router.replace(this.$route.query.redirect)
        } else {
          this.$router.replace('/layout/home')
        }
      } catch (error) {
      //   登录失败就可以提示
        Notify({ type: 'danger', message: '手机号或密码不正确' })
      }
      //   加载完毕
      this.isLoading = false
    }
  }
}
</script>
<style lang="less" scoped>
// 进行样式的覆盖
/deep/.van-nav-bar {
  background-color: #007bff;
}
</style>
